package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.itcast.mapper.PermissionMapper;
import com.itcast.mapper.RoleMapper;
import com.itcast.mapper.UserMapper;
import com.itcast.pojo.Permission;
import com.itcast.pojo.Role;
import com.itcast.pojo.User;
import com.itcast.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public User findUserByUsername(String username) {
        User user = userMapper.findUserByUsername(username);
        Set<Role> roles = roleMapper.findRolesByUserId(user.getId());
        for (Role role : roles) {
            Set<Permission> permissions = permissionMapper.findPermissionsByRoles(role.getId());
            role.setPermissions(permissions);
        }
        user.setRoles(roles);
        return user;
    }
}
